package cc.xuzhi.muggle.devtools.controller;

import cc.xuzhi.muggle.common.model.Result;
import cc.xuzhi.muggle.devtools.model.ColumnInfo;
import cc.xuzhi.muggle.devtools.model.GeneratorConfig;
import cc.xuzhi.muggle.devtools.model.TableInfo;
import cc.xuzhi.muggle.devtools.service.GeneratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/generator")
public class GeneratorController {

    @Autowired
    private GeneratorService service;

    @GetMapping("/test")
    public Result test() throws IOException {
        File file = new File(this.getClass().getResource("").getPath());
        String format = String.format("%s/%s", file.getCanonicalPath(), "muggle-admin/src/main/java/cc/xuzhi/muggle/admin/controller");
        return Result.ok(file.getCanonicalPath());
    }

    @GetMapping("/find/table/list")
    public Result<List<TableInfo>> findTableList() {
        return Result.ok(service.findTableList());
    }

    @GetMapping("/find/column/list")
    public Result<List<ColumnInfo>> findColumnList(@RequestParam("tableName") String tableName) {
        return Result.ok(service.findColumnList(tableName));
    }

    @GetMapping("/generator/{tableName}")
    public Result generator(@PathVariable("tableName") String tableName) throws IOException {
        GeneratorConfig config = new GeneratorConfig();
        config.setTableName(tableName);
        config.setPackageName("cc.xuzhi.muggle.admin");
        config.setAuthor("xuzhi");
        config.setModuleName("muggle-admin");
        config.setEnableSwagger(true);
        return Result.ok(service.generator(config));
    }
}
